//
//  BaseViewController.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/6/13.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "BaseViewController.h"
#import "CKAlertViewController.h"
#import "WebViewController.h"
#import <SystemConfiguration/CaptiveNetwork.h>


@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getWiFiNameFun) name:@"researchWifi" object:nil];
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;   //启用侧滑手势

}

//获取当前WiFi名称
- (void)getWiFiNameFun{
    id info = nil;
    NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
    for (NSString *ifnam in ifs) {
        info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
        NSString *str = info[@"SSID"];
        //        NSString *str2 = info[@"BSSID"];
        //        NSString *str3 = [[ NSString alloc] initWithData:info[@"SSIDDATA"] encoding:NSUTF8StringEncoding];
        
        if ([BGFunctionHelper isNULLOfString:str]) {
            return;
        }
        
        if ([str rangeOfString:@"ChongDianDian"].location !=NSNotFound) {
            //包含
            [self alertVCByTitle:[NSString stringWithFormat:@"当前连接WiFi：\n%@\n是否前去配置WiFi",str]];
        }
        
    }
}

- (void)alertVCByTitle:(NSString *)title{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:title preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIStoryboard *storyboard = kMainStroyboard;
        WebViewController *webView = [storyboard instantiateViewControllerWithIdentifier:@"WebViewController"];
        webView.titleString = @"配置设备WiFi";
        webView.urlString = @"http://192.168.4.1/wifi_set.html";
        [self.navigationController pushViewController:webView animated:YES];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:cancelAction];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)checkVerson:(NewVersonModel *)newVersonModel{
    
    NSString *newVerson = newVersonModel.ios_number;
    
    NSString *myAppVerson = kAppVersion;
    NSArray *myAppVersonArray = [myAppVerson componentsSeparatedByString:@"."];
    
    NSLog(@"myAppVersonArray -> %@",myAppVersonArray);
    
    NSArray *newVersonArray = [newVerson componentsSeparatedByString:@"."];
    NSLog(@"newVersonArray -> %@",newVersonArray);
    
    if (!myAppVersonArray) {
        return;
    }
    if (!newVersonArray) {
        return;
    }
    
    for (NSInteger i = 0; i < myAppVersonArray.count; i ++) {
        NSString *num1 = myAppVersonArray[i];
        NSString *num2 = newVersonArray[i];
        if (num1.integerValue < num2.integerValue) {
            NSString *contentString;
            if (![BGFunctionHelper isNULLOfString:newVersonModel.content]) {
                contentString = [newVersonModel.content stringByReplacingOccurrencesOfString:@";" withString:@";\n"];
            }
            
            CKAlertViewController *alertVC = [CKAlertViewController alertControllerWithTitle:@"发现新版本" message:contentString ];
            alertVC.messageAlignment = NSTextAlignmentLeft;
            
            CKAlertAction *update = [CKAlertAction actionWithTitle:@"立即更新" handler:^(CKAlertAction *action) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:newVersonModel.ios_url]];
                
                if (newVersonModel.update_status.integerValue == 0) {
                    [alertVC dismissViewControllerAnimated:YES completion:nil];
                }
            }];
            
            
            if (newVersonModel.update_status.integerValue == 0) {
                CKAlertAction *cancel = [CKAlertAction actionWithTitle:@"暂不更新" handler:^(CKAlertAction *action) {
                    [alertVC dismissViewControllerAnimated:YES completion:nil];
                }];
                [alertVC addAction:cancel];
            }
            
            
            [alertVC addAction:update];
            
            [self presentViewController:alertVC animated:NO completion:nil];
            break;
        }
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}


- (void)creatImageBackView:(NSMutableArray *)array{
    if (!self.imagebackView) {
        self.imagebackView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width/2, kScreenSize.width/2)];
        self.imagebackView.center = self.view.center;
        [self.view addSubview:self.imagebackView];
    }
    if (array.count > 0) {
        self.imagebackView.image = [UIImage imageNamed:@""];
    }else{
        self.imagebackView.image = [UIImage imageNamed:@"meishuju"];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
